package VerticalPrototypes;

import java.sql.*;

/**
 * @author Tyler
 */

//Class for fetching a user's username by ID number.
public class GetUsernameByID {
    
    //Instance of this class being used.
    private static GetUsernameByID instance = null;
    
    public GetUsernameByID()
    {
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        //Hardcoded database location.
        String url = "snyamathi.dyndns.org:37965";
        String user = "root";
        String password = "baconatmidnight";
        
        //Hardcoded prototype username to get ID of.
        String username = "";
        int user_id = 1;
        try {
            //Connect to the databse.
            con = DriverManager.getConnection(url, user, password);
            st = con.createStatement();

            //get user's ID.
				//Tyler's manual test string: SELECT username FROM accounts WHERE userID = 1;
            if(rs == (st.executeQuery("SELECT username FROM accounts WHERE userID = " + user_id)))
            { /*Success; got userID */ 
                username = (rs.getString(1));
            }
            else{ /*Not success.*/ }

            //Prints out results.
            if (rs.next()) {
                System.out.println(username);
            }

        } catch (SQLException ex) {
                //Something happens.
        } finally {
            //Close connections safely - May not need to do this? Overkill?
            try {
                if (rs != null) {
                    rs.close();
                }
                if (st != null) {
                    st.close();
                }
                if (con != null) {
                    con.close();
                }

            } catch (SQLException ex) {
                    //Something happens.
            }
        }
    }
    
    //Execute the addition of item to global database.
    public static GetUsernameByID user_GetUsernameByID()
    {
        if(instance == null)
            instance = new GetUsernameByID();
        return instance;
    }  
}